package com.gxlu.ms.base.util;

import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import com.gxlu.ms.base.module.context.RpcContext ;
/**
 *
 * @author lhc
 * @date 2018/1/26
 */
public class TraceUtil {

    private static String buildTraceId(Long userId){
        String uuid = UUID.randomUUID().toString();
        if(userId!=null && userId > 0){
            uuid += userId;
        }
        String[] array =uuid.split("-");
        StringBuilder sb = new StringBuilder();
        for(String str:array){
            sb.append(str);
        }
        return sb.toString();
    }

    public static String getTraceId(){
        return  getRpcContext().getTraceId() ;
    }

    public static String getPerssureTest(){
        return  getUserData(RpcContext.PERSIST_TEST);
    }

    public static  void putUserData(String key,String value){
        if(StringUtils.isBlank(key)){
            return ;
        }

        getRpcContext().putAttribute(key,value);
    }

    public static String getUserId(){
        return getRpcContext().getAttribute(RpcContext.USER_ID);
    }

    public static String getUserData(String key){
        if(StringUtils.isBlank(key)){
            return  "";
        }
        return getRpcContext().getAttribute(key);
    }

    public static void removeUserData(String key){
        if(StringUtils.isBlank(key)){
            return  ;
        }
        getRpcContext().removeAttribute(key);
    }

    private static  RpcContext getRpcContext(){
        if(RpcContext.get()==null){
            startTrace(0L);
        }
        return RpcContext.get();
    }

    public static Map<String,String> getAttribute(){
        return getRpcContext().getAttributes();
    }

    /**
     * finally中一定要调用这个
     * 一定要清除，否则会有内存泄漏
     */
    public static void remove(){
        if(RpcContext.get()!=null) {
            RpcContext.set(null);
        }
    }

    public static void setStartTime(long startTime){
        getRpcContext().setStartTime(startTime);
    }

    public static long getStartTime(){
        return getRpcContext().getStartTime();
    }

    /**
     * 开始
     * 记录日志初化化一定要先设置一个 traceId
     * @param userId
     */
    public static  void startTrace(Long userId){
        RpcContext rpcContext = RpcContext.get();
        if(rpcContext==null){
             rpcContext = new RpcContext(buildTraceId(userId));
        }
        RpcContext.set(rpcContext);
    }

    /**
     * 用于上下游应用透传traceId时使用
     * @param traceId
     */
    public static  void setTrace(String traceId){
        RpcContext rpcContext = RpcContext.get();
        if(rpcContext==null){
            if(StringUtils.isNotBlank(traceId)) {
                rpcContext = new RpcContext(traceId);
            }else{
                rpcContext = new RpcContext(buildTraceId(0L));
            }
        }
        RpcContext.set(rpcContext);
    }

    public static  void startTrace(){
         startTrace(0L);
    }

    public static void main(String[] args){
        TraceUtil.startTrace(333L);
        TraceUtil.putUserData("u1","123");
        TraceUtil.putUserData(RpcContext.USER_ID,"3333");
        String tradeId = TraceUtil.getTraceId();
        System.out.println(tradeId);
        System.out.println(TraceUtil.getAttribute());

        Thread t = new Thread(){
            @Override
            public void run() {
                TraceUtil.startTrace(444L);
                TraceUtil.putUserData("u1","4455");
                TraceUtil.putUserData(RpcContext.USER_ID,"7777");
                System.out.println(TraceUtil.getTraceId());
                System.out.println(TraceUtil.getAttribute());
            }
        };
        t.start();
    }

}
